mui.init({

});

//倒计时  
var countdown = 60;

function settime(val) {
    var num = parseInt(document.getElementById("phoneNum").value);
    check_mobile(num, val);
}

mui.plusReady(function () {
    var nextBtn = document.getElementById('nextBtn');
    var phoneNum = document.getElementById('phoneNum');
    var verCode = document.getElementById('verCode');
    nextBtn.addEventListener('tap', function () {
        if (phoneNum.value.length == 0) {
            mui.toast('手机号不能为空', {
                duration: '2000',
                type: 'div'
            });
            return false;
        } else if (verCode.value.length == 0) {
            mui.toast('验证码不能为空', {
                duration: '2000',
                type: 'div'
            });
            return false;
        } else if (parseInt(verCode.value) != parseInt(plus.storage.getItem('mob_code'))) {
            mui.toast('验证码输入错误', {
                duration: '2000',
                type: 'div'
            });
            return false;
        } else {
            plus.storage.setItem('mob_code', '00000000');
            mui.openWindow({
                url: 'finishReg.html',
                extras: {
                    'mobile': phoneNum.value,
                    'code': verCode.value
                },
                createNew: false,
            })
        }
    });
});

//验证手机是否可以用
function check_mobile(num, val) {
    var api_host = plus.storage.getItem('HOST_URL');
    mui.ajax({
        url: api_host + '/api/linxuan/user/checkmobile',
        data: {
            'mobile': num,
        },
        dataType: 'json', //服务器返回json格式数据
        type: 'post', //HTTP请求类型
        timeout: 2000, //超时时间设置为10秒；
        success: function (msg) {
            if (msg.code == 1) {
                if (countdown == 0) {
                    clearInterval(in1);
                    val.removeAttribute("disabled");
                    val.value = "获取验证码";
                    countdown = 60;
                    return false;
                } else {
                    var in1 = setInterval(function () {
                        val.setAttribute("disabled", true);
                        val.value = countdown + "s后重新发送";
                        countdown--;
                        if (countdown == 0) {
                            clearInterval(in1);
                            val.removeAttribute("disabled");
                            val.value = "获取验证码";
                            countdown = 60;
                            return false;
                        }
                    }, 1000)
                }
                fs_code(num);
            } else {
                mui.toast(msg.msg, {
                    duration: '2000',
                    type: 'div'
                });
            }
        },
        error: function (xhr, type, errorThrown) {
            mui.toast('检测手机号失败，请联系客服！', {
                duration: '2000',
                type: 'div'
            });
        }
    });
}

//发送验证码
function fs_code(num) {
    var api_host = plus.storage.getItem('HOST_URL');
    var user_token = plus.storage.getItem('user_token');
    mui.ajax({
        url: api_host + '/api/linxuan/user/sendverify',
        data: {
            'account': num,
            'user_token': user_token
        },
        dataType: 'json',
        type: 'post',
        timeout: 10000,
        success: function (msg) {
            console.log(msg);
            if (msg.code == 1) {
                plus.storage.removeItem('mob_code');
                var mob_code = msg.data;
                plus.storage.setItem('mob_code', mob_code.toString());
                mui.toast(msg.msg, {
                    duration: '1000',
                    type: 'div'
                });
            } else {
                mui.toast(msg.msg, {
                    duration: '1000',
                    type: 'div'
                });
            }
        },
        error: function (xhr, type, errorThrown) {
            mui.toast('系统错误，请联系客服！', {
                duration: '2000',
                type: 'div'
            });
        }
    });
}

